/*
 * @(#)QueryFocusControl.java Jan 8, 2012 - 1:40:31 AM
 * 
 * Copyright 2011 Cin/UFPE/VSPEED
 */
package br.ufpe.cin.vspeed.view;

import java.awt.event.MouseEvent;

import prefuse.controls.FocusControl;
import prefuse.visual.VisualItem;
import prefuse.visual.tuple.TableNodeItem;
import br.ufpe.cin.vspeed.gui.PanelChooseDomain;
import br.ufpe.cin.vspeed.gui.PanelSparQL;
import br.ufpe.cin.vspeed.gui.PanelVisualQuery;

/**
 * @author <a href="mailto:andreza.lba@gmail.com">Andreza Leite de Alencar</a>
 */
public class QueryFocusControl extends FocusControl {
	private final Object target;

	public QueryFocusControl(final int i, final Object target) {
		super(i);
		this.target = target;
	}

	@Override
	public void itemClicked(final VisualItem visualItem, final MouseEvent mouseEvent) {
		super.itemClicked(visualItem, mouseEvent);

		if (mouseEvent.getClickCount() == 2) {
			if (visualItem instanceof TableNodeItem) {
				final TableNodeItem selectedNode = (TableNodeItem) visualItem;
				final String str = selectedNode.toString();
				if (this.target instanceof PanelVisualQuery) {
					final PanelVisualQuery panelVisualQuery = (PanelVisualQuery) this.target;

					// VisualItem[graph.nodes,3,URI=http://ontology1237476463.owl#ResearchGroup,name=ResearchGroup,type=class]
					final String[] strArray = str.split(",");
					final String conceptName = strArray[3].split("=")[1];
					panelVisualQuery.updateTxtDLArea(conceptName);
				} else if (this.target instanceof PanelChooseDomain) {
					final PanelChooseDomain panelChooseDomain = (PanelChooseDomain) this.target;
					final Integer id = (Integer) visualItem.get("ident");
					final Flavour flavor = (Flavour) visualItem.get("ftype");
					final String type = flavor.getFlavourType();

					panelChooseDomain.notifySelectedNode(id, type);
				} else if (this.target instanceof PanelSparQL) {
					final PanelSparQL panelSparqlQuery = (PanelSparQL) this.target;

					// VisualItem[graph.nodes,3,URI=http://ontology1237476463.owl#ResearchGroup,name=ResearchGroup,type=class]
					final String[] strArray = str.split(",");
					final String conceptName = strArray[3].split("=")[1];
					panelSparqlQuery.updateQuery(conceptName);
				}
			}
		}
	}
}
